package com.yanqu.road.logic.cross;

import com.yanqu.road.entity.decoration.MammonRecord;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.logic.sort.MammonRecordSort;
import com.yanqu.road.pb.decoration.DecorationProto;

import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossGroupMammonRecordListModel {

    private int listNum = GameConfig.MAMMON_RECORD_MAX_COUNT;

    private MammonRecordSort mammonRecordSort;

    private boolean isNeedSync = false;

    private LinkedList<MammonRecord> recordList;
    private Map<String, MammonRecord> recordMap;

    private int mammonDecorationId;

    private int groupId;

    private DecorationProto.MammonRecordListMsg.Builder recordListMsg;

    public CrossGroupMammonRecordListModel(int groupId, int mammonDecorationId) {
        mammonRecordSort = new MammonRecordSort();
        recordMap = new ConcurrentHashMap<>();
        recordList = new LinkedList<>();
        recordListMsg = DecorationProto.MammonRecordListMsg.newBuilder();
        this.groupId = groupId;
        this.mammonDecorationId = mammonDecorationId;
    }

    public int getGroupId() {
        return groupId;
    }

    public int getMammonDecorationId() {
        return mammonDecorationId;
    }

    public boolean isNeedSync() {
        return isNeedSync;
    }

    public void setNeedSync(boolean needSync) {
        isNeedSync = needSync;
    }

    public DecorationProto.MammonRecordListMsg.Builder getRankListMsg() {
        return recordListMsg.clone();
    }

    public LinkedList<MammonRecord> getRecordList() {
        return recordList;
    }

    public void setRecordMap(Map<String, MammonRecord> recordMap) {
        synchronized (this) {
            this.recordMap = recordMap;
            this.recordList = new LinkedList<>(recordMap.values());
            recordList.sort(mammonRecordSort);
            int index = 1;
            for(MammonRecord mammonRecord : recordList){
                mammonRecord.setMammonIndex(index);
                index++;
            }
            initRecordMsg();
        }
    }

    private void initRecordMsg() {
        LinkedList<MammonRecord> dataList;
        if(recordList.size() > listNum){
            dataList = new LinkedList<>(recordList.subList(recordList.size() - listNum, recordList.size()));
        }else {
            dataList = new LinkedList<>(recordList);
        }
        recordListMsg = DecorationPb.parseMammonRecordListMsg(dataList);
    }

    /**
     * 添加到排行
     */
    public void recordChange(MammonRecord mammonRecord) {
        if(!recordMap.containsKey(mammonRecord.getRecordUid())) {
            synchronized (this) {
                if (!recordMap.containsKey(mammonRecord.getRecordUid())) {
                    if(recordList.size() <= 0){
                        mammonRecord.setMammonIndex(1);
                        recordList.addLast(mammonRecord);
                    }else if (mammonRecord.getRecordTime() > recordList.getLast().getRecordTime()) {
                        MammonRecord tempRecord = recordList.getLast();
                        int index = tempRecord.getMammonIndex();
                        mammonRecord.setMammonIndex(index + 1);
                        recordList.addLast(mammonRecord);
                    } else if (mammonRecord.getRecordTime() < recordList.getFirst().getRecordTime()) {
                        MammonRecord tempRecord = recordList.getFirst();
                        int index = tempRecord.getMammonIndex();
                        recordList.addFirst(mammonRecord);
                        for(MammonRecord record : recordList){//重新生成下标
                            record.setMammonIndex(index);
                            index++;
                        }
                    } else {
                        MammonRecord tempRecord = recordList.getFirst();
                        int index = tempRecord.getMammonIndex();
                        recordList.addLast(mammonRecord);
                        recordList.sort(mammonRecordSort);
                        for(MammonRecord record : recordList){//重新生成下标
                            record.setMammonIndex(index);
                            index++;
                        }
                    }
                    initRecordMsg();
                    recordMap.put(mammonRecord.getRecordUid(), mammonRecord);
                    isNeedSync = true;
                }
            }
        }
    }
}
